﻿namespace Chunker.Inpe {
    using System;
    using System.IO;
    using System.Linq;
    using System.Threading;
    using Chunker.Inpe.Data.SqlServer;

    class Program {
        static void Main(string[] args) {
            //ThreadPool.SetMaxThreads(20, 1000);
            //ChunkFiles(args);
            //DeleteFiles();
            PngChunker.ChunkFiles(@"C:\Projetos\FileChunker");
        }

        static void Analyse() {
            InpeAnalyser.CountChunkColition(3);
        }

        static void ChunkFiles(string[] args) {
            InpeChunker.ChunkFiles(@"C:\Projetos\FileChunker");
        }

        static void CopyFiles(string pathSource, string pathDestination, int startDay, int endDay) {
            foreach (string file in Directory.GetFiles(pathSource)) {
                string strDay = file.Substring(file.IndexOf("201004") + 6, 2);
                int day = int.Parse(strDay);
                if (startDay <= day && endDay >= day) {
                    string fileName = new FileInfo(file).Name;
                    File.Copy(file, Path.Combine(pathDestination, fileName));
                }
            }
        }

        static void DeleteFiles() {
            using (InpeEntitiesSqlServer entities = new InpeEntitiesSqlServer()) {
                var configurations = entities
                                        .Configuration
                                        .Include("Images")
                                        .Include("Images.ImageChunk")
                                        .Where(x => x.IdConfiguration ==15);
                foreach (Configuration configuration in configurations) {
                    foreach (Image image in configuration.Images.ToList()) {
                        foreach (ImageChunk chunk in image.ImageChunk.ToList()) {
                            entities.DeleteObject(chunk);
                        }
                        entities.DeleteObject(image);
                    }
                    entities.DeleteObject(configuration);
                }
                entities.SaveChanges();
            }
        }
    }
}
